package com.centaline.androidsalesblog.activities.sale;

import android.view.Menu;
import android.view.MenuItem;

import com.android.volley.VolleyError;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.adapter.EsfDealHistoryAdapter;
import com.centaline.androidsalesblog.api.sale.EsfDealHistoryApi;
import com.centaline.androidsalesblog.base.BaseActivity;
import com.centaline.androidsalesblog.constants.ExtraConstant;
import com.centaline.androidsalesblog.iml.DropListener;
import com.centaline.androidsalesblog.model.businissobject.DropBo;
import com.centaline.androidsalesblog.model.businissobject.EsfDealHistoryBo;
import com.centaline.androidsalesblog.model.responseobject.EsfDealHistoryRo;
import com.centaline.androidsalesblog.utils.DropUtil;
import com.centaline.androidsalesblog.widgets.MRecyclerView;
import com.centaline.androidsalesblog.widgets.dropdown.SingleDrop;
import com.centanet.framework.interfaces.IRecycleViewListener;
import com.centanet.framework.interfaces.IResponseListener;

import java.util.ArrayList;
import java.util.Locale;

/**
 * 小区成交历史
 */
public class EsfDealHistoryActivity extends BaseActivity {

    private String[] roomCount = new String[]{"不限", "一室", "二室", "三室", "四室", "五室", "五室以上"};
    private MRecyclerView mRecyclerView;
    private ArrayList<EsfDealHistoryBo> list = new ArrayList<>();
    private int iRefreshType;
    private EsfDealHistoryApi esfDealHistoryApi;
    private String menuString;
    private SingleDrop singleDrop;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_esf_deal_history;
    }

    @Override
    protected void findViews() {
        setUniversalToolbar(R.string.title_est_deal_history);
        mRecyclerView = (MRecyclerView) findViewById(R.id.mRecyclerView);
    }

    @Override
    protected void initViews() {
        singleDrop = new SingleDrop(findViewById(R.id.appbar), this);
        singleDrop.setDropListener(new DropListener() {
            @Override
            public void dropComplete(boolean fromMore, int type, DropBo... dropBos) {
                if (dropBos.length == 1) {
                    if (DropUtil.compareValue(dropBos[0])) {
                        menuString = "户型";
                        esfDealHistoryApi.setMinroomcnt("");
                        esfDealHistoryApi.setMaxroomcnt("");
                    } else if ("6".equals(dropBos[0].getValue())) {
                        menuString = dropBos[0].getText();
                        esfDealHistoryApi.setMinroomcnt("6");
                        esfDealHistoryApi.setMaxroomcnt("50");
                    } else {
                        menuString = dropBos[0].getText();
                        esfDealHistoryApi.setMinroomcnt(dropBos[0].getValue());
                        esfDealHistoryApi.setMaxroomcnt(dropBos[0].getValue());
                    }
                    mRecyclerView.startRefresh();
                    invalidateOptionsMenu();
                }
            }
        });
        mRecyclerView.setAdapter(new EsfDealHistoryAdapter(this, list));
        mRecyclerView.setIRecycleViewListener(new IRecycleViewListener() {
            @Override
            public void downRefresh() {
                iRefreshType = 1;
                esfDealHistoryApi.setStartIndex(0);
                request();
            }

            @Override
            public void upRefresh() {
                iRefreshType = 2;
                esfDealHistoryApi.setStartIndex(list.size());
                request();
            }

            @Override
            public void onItemClick(int position) {

            }
        });
        esfDealHistoryApi = new EsfDealHistoryApi(EsfEstateDetailActivity.class, EsfDealHistoryRo.class,
                new IResponseListener<EsfDealHistoryRo>() {


                    @Override
                    public void onResponse(EsfDealHistoryRo esfDealHistoryRo) {
                        final ArrayList<EsfDealHistoryBo> temp = esfDealHistoryRo.getList();
                        if (iRefreshType == 1)
                            list.clear();
                        if (temp == null) {
                            if (iRefreshType == 1) {
                                mRecyclerView.stopRefresh(false);
                            } else {
                                mRecyclerView.refreshError();
                            }
                        } else {
                            list.addAll(temp);
                            mRecyclerView.stopRefresh(temp.size() == 10);
                        }
                        if (iRefreshType == 1) {
                            snack(esfDealHistoryRo.getTotal() == 0 ?
                                    "没有找到成交历史" : String.format(Locale.CHINA, "共为您找到%d条成交历史", esfDealHistoryRo.getTotal()));
                        }
                    }

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        mRecyclerView.refreshError();
                    }
                }
        );
        esfDealHistoryApi.setCestCode(getIntent().getStringExtra(ExtraConstant.CestCode));
        ArrayList<DropBo> dropBos = new ArrayList<>();
        DropBo dropBo;
        int size = roomCount.length;
        for (int i = 0; i < size; i++) {
            dropBo = new DropBo(40);
            dropBo.setValue(String.valueOf(i));
            dropBo.setText(roomCount[i]);
            dropBos.add(dropBo);
        }
        singleDrop.init(dropBos);
        mRecyclerView.startRefresh();
        menuString = getString(R.string.menu_deal_default);
    }

    private void request() {
        esfDealHistoryApi.startRequest();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_deal_history, menu);
        MenuItem menuItem = menu.findItem(R.id.action_deal);
        menuItem.setTitle(menuString);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_deal) {
            singleDrop.show();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
